package com.phpxiu.app.kkylive.wxapi;

import com.phpxiu.app.kkylive.KKYApp;
import com.phpxiu.app.model.event.WeiXinPayEvent;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.BaseLogin;
import com.sina.weibo.sdk.api.share.Base;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import de.greenrobot.event.EventBus;
/**
 * @file
 * @brief  微信支付回调界面
 * @author 吴金洲
 * @note 微信支付回调界面
 *
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	api = WXAPIFactory.createWXAPI(this, BaseLogin.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@Override
	public void onResp(BaseResp resp) {
        WeiXinPayEvent event=null;
        switch (resp.errCode){
            case BaseResp.ErrCode.ERR_OK:
                KKYUtil.log("微信支付成功");
                if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
                    event=new WeiXinPayEvent(WeiXinPayEvent.PAY_SUCCESS,null);
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                KKYUtil.log("微信支付被取消");
                event=new WeiXinPayEvent(WeiXinPayEvent.PAY_CANCEL,null);
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                KKYUtil.log("微信支付未授权");
                event=new WeiXinPayEvent(WeiXinPayEvent.PAY_ERR,"auth denied!");
                break;
            case BaseResp.ErrCode.ERR_COMM:
                KKYUtil.log("微信支付未授权");
                EventBus.getDefault().post(new WeiXinPayEvent(WeiXinPayEvent.PAY_ERR,resp.errStr));
                break;
        }
        if(event!=null){
            EventBus.getDefault().post(event);
        }
        finish();
	}
}